package core;

import java.awt.event.*;
import java.util.concurrent.*;
import javax.swing.*;

public class Test {
    public static void main(String[] args) {
        new DownloadWin();
    }
}

//下载窗口，有一内部类Task继承了Thread
class DownloadWin extends JFrame {

    // 进度条，[0,100]
    private JProgressBar pb = new JProgressBar(0, 100);
    // 开始按钮：
    private JButton start = new JButton("开始");

    public DownloadWin() {
        super("下载进度");
        setSize(1200, 150);
        setLocation(500, 300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(null);

        start.setBounds(5, 45, 90, 30);
        add(start);
        // 点击开始按钮后：
        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                pb.setValue(0);// 进度条初始进度为0
//				new Task().start();//这一行可以代替下面三行代码
                ExecutorService exec = Executors.newCachedThreadPool();
                exec.execute(new Task());
                exec.shutdown();
            }
        });

        pb.setBounds(100, 35, 1000, 50);
        pb.setStringPainted(true);// 字符串显示进度
        add(pb);

        setVisible(true);
    }

    private class Task extends Thread {

        public void run() {
            while (pb.getValue() < pb.getMaximum()) {

                try {
                    sleep(100);
                    pb.setValue(pb.getValue() + 1);
                } catch (Exception e) {
                }
            }
            start.setEnabled(true);
            start.setText("重新下载");
        }
    }
}